/**
 *                            License
 * THE WORK (AS DEFINED BELOW) IS PROVIDED UNDER THE TERMS OF THIS  
 * CREATIVE COMMONS PUBLIC LICENSE ("CCPL" OR "LICENSE"). 
 * THE WORK IS PROTECTED BY COPYRIGHT AND/OR OTHER APPLICABLE LAW.  
 * ANY USE OF THE WORK OTHER THAN AS AUTHORIZED UNDER THIS LICENSE OR  
 * COPYRIGHT LAW IS PROHIBITED.
 * 
 * BY EXERCISING ANY RIGHTS TO THE WORK PROVIDED HERE, YOU ACCEPT AND  
 * AGREE TO BE BOUND BY THE TERMS OF THIS LICENSE. TO THE EXTENT THIS LICENSE  
 * MAY BE CONSIDERED TO BE A CONTRACT,
 * THE LICENSOR GRANTS YOU THE RIGHTS CONTAINED 
 * HERE IN CONSIDERATION OF YOUR ACCEPTANCE OF SUCH TERMS AND CONDITIONS.
 * 
 */
package lineage.packet.client;

import lineage.core.L1GameClient;
import lineage.model.instance.L1PcInstance;
import lineage.packet.L1AbstractPacketClient;
import lineage.packet.server.S_CharLock;

/**
 * 处理客户端封包：请求移动角色.
 * 
 * @version 2014年3月26日下午6:26:05
 * @author jrwz
 */
public final class C_CharMove extends L1AbstractPacketClient {

    @Override
    public void execute(final L1GameClient client, final byte[] data) {
        try {
            this.init(data);

            final L1PcInstance pc = client.getOnlineChar();
            if (pc == null) {
                return;
            }

            pc.getMap().setPassable(pc.getLocation(), true); // 解除旧坐标障碍(该位置允许通过)

            int x = 0; // x坐标
            int y = 0; // y坐标
            int heading = 0; // 面向

            try {
                x = this.readH();
                y = this.readH();
                heading = this.readC();
                heading = Math.min(heading, 7);
            } catch (final Exception e) {
                return;
            }

            boolean flag = pc.toMove(x, y, heading);
            if (!flag) {
                pc.sendPackets(new S_CharLock()); // 发送回溯封包
                return;
            }
            pc.updateObject(); // TODO 更新可见对象
        } catch (final Exception e) {
        } finally {
            this.close();
        }
    }
}
